/*
 * SoundManager.java
 *
 * Created on 4. Februar 2007, 21:01
 *
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.util;

import com.jme.system.DisplaySystem;
import com.jmex.sound.openAL.SoundSystem;
import kanjitori.events.AbstractListener;
import kanjitori.events.BotKilledEvent;
import kanjitori.events.ItemCollectedEvent;
import kanjitori.events.ManagerService;
import kanjitori.events.PlayerRelocateEvent;
import kanjitori.graphics.Thing;

/**
 *
 * @author Pirx
 */
public class SoundManager {
    
    private static final SoundManager INSTANCE = new SoundManager();
    
    private static final String BOT_KILLED_SAMPLE = "kanjitori/res/sfx/gasgrenade.ogg";
    private static final int BOT_KILLED = 1;
    private int botKilledSound;
    
    private static final String ITEM_COLLECTED_SAMPLE = "kanjitori/res/sfx/am_pkup.wav";
    private static final int ITEM_COLLECTED = 2;
    private int itemCollectedSound;

    private static final String PLAYER_RELOCATE_SAMPLE = "kanjitori/res/sfx/respawn1.wav";
    private static final int PLAYER_RELOCATE = 3;
    private int playerRelocateSound;
    
    private boolean on = true;
    private int snode;
    
    
    /** Creates a new instance of SoundManager */
    private SoundManager() {
        initSound();
        ManagerService.register(new AbstractListener<BotKilledEvent>(BotKilledEvent.class){
            public void notify(BotKilledEvent event) {
                playSound(event.getContent(), botKilledSound, BOT_KILLED);
            }
        });
        ManagerService.register(new AbstractListener<ItemCollectedEvent>(ItemCollectedEvent.class){
            public void notify(ItemCollectedEvent event) {
                playSound(event.getContent(), itemCollectedSound, ITEM_COLLECTED);
            }
        });
        ManagerService.register(new AbstractListener<PlayerRelocateEvent>(PlayerRelocateEvent.class){
            public void notify(PlayerRelocateEvent event) {
                playSound(event.getContent(), playerRelocateSound, PLAYER_RELOCATE);
            }
        });
    }
    
    public boolean isOn() {
        return on;
    }
    
    public void setOn(boolean on) {
        this.on = on;
    }
    
    
    public static SoundManager getManager() {
        return INSTANCE;
    }
    
    public void draw() {
        SoundSystem.draw(snode);
    }
    
    private void initSound() {
        SoundSystem.init(DisplaySystem.getDisplaySystem().getRenderer().getCamera(),
                SoundSystem.OUTPUT_DEFAULT);
        snode = SoundSystem.createSoundNode();
        
        botKilledSound = createSample(BOT_KILLED_SAMPLE, BOT_KILLED);
        itemCollectedSound = createSample(ITEM_COLLECTED_SAMPLE, ITEM_COLLECTED);
        playerRelocateSound = createSample(PLAYER_RELOCATE_SAMPLE, PLAYER_RELOCATE);
    }
    
    private int createSample(String sample, int eventID) {
        int sampleID = SoundSystem.create3DSample(SoundManager.class
                .getClassLoader().getResource(sample));
        SoundSystem.bindEventToSample(sampleID, eventID);
        SoundSystem.addSampleToNode(sampleID, snode);
        return sampleID;
    }
    
    private void playSound(Thing thing, int sampleID, int eventID) {
        if (on) {
           SoundSystem.setSamplePosition(sampleID,
                thing.getNode().getLocalTranslation().x,
                thing.getNode().getLocalTranslation().y,
                thing.getNode().getLocalTranslation().z);
           SoundSystem.onEvent(eventID);
        }
    }
    
    public void update(float tpf) {
       SoundSystem.update(tpf); 
    }
}

